// Framework.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include "SceneManager.h"
#include "ResourceManager.h"
#include "Camera.h"
#include "KeyManager.h"
#include "VideoDriver.h"
#include "Utils.h"

int Init ( ESContext *esContext )
{
	glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );

	char *url_res = JoinString(CONFIG_PATH, "ResrouceManager.txt");
	char *url_sce = JoinString(CONFIG_PATH, "SceneManager.txt");

	cResourceManager::GetInstance()->LoadResource(url_res);
	cSceneManager::GetInstance()->LoadScene(url_sce);

	SAFE_DEL_ARRAY(url_res);
	SAFE_DEL_ARRAY(url_sce);

	cSceneManager::GetInstance()->Init();
	cVideoDriver::GetInstance()->Init();

	return 0;
}

void Draw ( ESContext *esContext )
{
	glClear(GL_COLOR_BUFFER_BIT);

	cVideoDriver::GetInstance()->SetFBO(0);
	cSceneManager::GetInstance()->Render();
	cVideoDriver::GetInstance()->StopFBO();

	cVideoDriver::GetInstance()->DrawPostProcessingEffects(0);

	eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
}

void Update ( ESContext *esContext, float deltaTime )
{
	if (CKeyManager::GetInstance()->WasKeyDown(KEY_W))
	{
		cCamera::GetInstance()->MoveForward();
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_S))
	{
		cCamera::GetInstance()->MoveBack();
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_A))
	{
		cCamera::GetInstance()->MoveLeft();
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_D))
	{
		cCamera::GetInstance()->MoveRight();
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_UP))
	{
		cCamera::GetInstance()->RotateX(RADIAN(0.7f));
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_DOWN))
	{
		cCamera::GetInstance()->RotateX(RADIAN(-0.7f));
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_LEFT))
	{
		cCamera::GetInstance()->RotateY(RADIAN(0.7f));
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_RIGHT))
	{
		cCamera::GetInstance()->RotateY(RADIAN(-0.7f));
	}
	else if (CKeyManager::GetInstance()->WasKeyDown(KEY_F))
	{
		cVideoDriver::GetInstance()->m_typeEffect++;
		if (cVideoDriver::GetInstance()->m_typeEffect > cVideoDriver::PPE_DEPTH_OFF_FIELD)
		{
			cVideoDriver::GetInstance()->m_typeEffect = cVideoDriver::PPE_FBO;
		}
		CKeyManager::GetInstance()->Clear();
	}

	cCamera::GetInstance()->ComputeView();

	cSceneManager::GetInstance()->Update(deltaTime);
}

void Key ( ESContext *esContext, unsigned char key, bool bIsPressed )
{
	CKeyManager::GetInstance()->Update(bIsPressed, key);
}

void CleanUp()
{
	cSceneManager::DestroyInstance();
	cResourceManager::DestroyInstance();
	cCamera::DestroyInstance();
	CKeyManager::DestroyInstance();
	cVideoDriver::GetInstance()->DestroyInstance();
}

int _tmain(int argc, _TCHAR* argv[])
{
	ESContext esContext;

    esInitContext ( &esContext );

	esCreateWindow ( &esContext, "Engine3D", SCREEN_W, SCREEN_H, ES_WINDOW_RGB);

	if ( Init ( &esContext ) != 0 )
		return 0;

	esRegisterDrawFunc ( &esContext, Draw );
	esRegisterUpdateFunc ( &esContext, Update );
	esRegisterKeyFunc ( &esContext, Key);

	esMainLoop ( &esContext );

	//releasing OpenGL resources
	CleanUp();

	//identifying memory leaks
	MemoryDump();
	printf("Press any key...\n");
	_getch();

	return 0;
}
